package com.ybfenpan.controller.member.wealth;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.log.LogEle;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.log.LogEleService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
 
public class MoneyZhuanController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogEleService logEleService;

	
	
	@Token(save=true)
	@RequestMapping("member/wealth/moneyZhuanShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);

		return "member/wealth/moneyZhuan";
	}


	@RequestMapping("member/wealth/moneyZhuan.shtml")
	@ResponseBody
	@Token(remove=true)
	public void buy(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception{
		
		Boolean result=true;
		String msg = "";
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());

		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
		Double num = MapUtil.getDoubleValue(formMap, "num");
    	if(!member.getTwoPassword().equals(pwd)){
    		msg = "安全密码不正确。";
    		result=false;
    	}else{
    		if (num<0){
    			msg="输入购买数量不正确！";
    			result=false;
			}
    	}
		
    	if(result){

			LogEle logEle=new LogEle();
			logEle.setMemberId(member.getId());
			logEle.setNum(num);
			logEle.setTitle("已提交后台，等待管理员审核！");
			logEle.setState(22);
			logEle.setYu(member.getEle());
			logEleService.saveOrUpdate(logEle);

    	}
    	
    	HashMap resultformMap = new HashMap();
		resultformMap.put("msg", msg);
		renderSuccess(response, result, resultformMap); 
		
	}

	@RequestMapping("member/wealth/ajaxGetMemberMoneyByloginName.shtml")
	@ResponseBody
	public void ajaxTuRecommendAction(HttpServletRequest request, HttpServletResponse response) {


		Map formMap = getParameterMap(request);
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);

		HashMap resultformMap = new HashMap();
		if(member==null){

			renderSuccess(response, false, resultformMap);
		}else{

			resultformMap.put("personName", member.getPersonName());

			renderSuccess(response, true, resultformMap);

		}

	}
}
